package com.yanqu.road.server.cmd.cricket;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cricketcourt.CrossCricketCourtMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = Protocol.C_CRICKET_GET_COURT_INFO, desc = "竞技场信息")
public class CrossCricketGetCourtInfoCmd extends CrossCommand {

    CricketProto.CrossCricketGetCourtInfoReqMsg reqMsg;

    long userId;

    private List<CricketProto.CricketMatchDataTemp> cricketDefenseList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CricketProto.CrossCricketGetCourtInfoReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        cricketDefenseList = reqMsg.getCricketDefenseList();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        // 更新userBaseInfo
        UserBaseInfo userBaseInfo = PlayerBasePb.parseBaseUserInfo(reqMsg.getUserBaseInfo());
        CrossUserMgr.updateUserBaseInfo(userId, userBaseInfo);
        ret = CrossCricketCourtMgr.getCourtInfo(serverId, userId, cricketDefenseList);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            CricketProto.CricketGetCourtInfoRespMsg.Builder crossResp = CricketProto.CricketGetCourtInfoRespMsg.newBuilder();
            crossResp.setRet(ret);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.U_CRICKET_GET_COURT_INFO, crossResp));
        }
    }
}
